//
//  FileTool.swift
//  ZipTools
//
//  Created by 齐庆斌 on 2018/4/13.
//  Copyright © 2018年 齐庆斌. All rights reserved.
//

import UIKit

// 根文件夹类型
enum DocumentType: String {
    case DocumentTypeDocument = "Documents"
    case DocumentTypeTmp = "tmp"
    case DocumentTypeCache = "Library/Caches"
    case DocumentTypePreferences = "Library/Preferences"
}

fileprivate let manager = FileManager.default

class FileTool: NSObject {
    
    fileprivate func getPath(path: String) -> String{
        return NSHomeDirectory() + "/" + path
    }
    
    fileprivate func baseFunc(path: String) -> [String]{
        let home = NSHomeDirectory() + "/" + path
        var paths: [String] = [String]()
        
        for path in manager.subpaths(atPath: home)! {
            if !path.hasPrefix("."){
                paths.append(path)
            }
        }
        return paths
    }
}

// 获取路径
extension FileTool {
    class func getDocument() -> String{
        return FileTool().getPath(path: DocumentType.DocumentTypeDocument.rawValue)
    }
    class func getTmp() -> String{
        return FileTool().getPath(path: DocumentType.DocumentTypeTmp.rawValue)
    }
}

// 获取路径下的所有文件
extension FileTool {
    // 获取document目录下的所有文件
    class func getAllFilesFromDocument() -> [String] {
        return FileTool().baseFunc(path: DocumentType.DocumentTypeDocument.rawValue)
    }
    // 获取tmp目录下的所有文件
    class func getAllFilesFromTmp() -> [String]{
        return FileTool().baseFunc(path: DocumentType.DocumentTypeTmp.rawValue)
    }
    class func getAllFilesFromCache() -> [String] {
        return FileTool().baseFunc(path: DocumentType.DocumentTypeTmp.rawValue)
    }
    class func getAllFilesFromPreferences() -> [String] {
        return FileTool().baseFunc(path: DocumentType.DocumentTypePreferences.rawValue)
    }
}

// 增删文件目录
extension FileTool {
    // 创建目录 文件夹名称；中间路径不存在是否自动创建
    class func createDirectory(name: String, isAutomicCreate: Bool) -> String {
        let home = FileTool.getDocument() + "/" + name
        // withIntermediateDirectories： 表示若中间路径不存在则创建
        do {
            try manager.createDirectory(atPath: home, withIntermediateDirectories: isAutomicCreate, attributes: nil)
        } catch {
            print("create failed")
        }
        return home
    }
    
    // 判断文件夹是否存在
    class func directionIsExistWithDirName(name: String) -> (String, Bool){
        if manager.fileExists(atPath: FileTool.getDocument() + "/" + name){
            return (FileTool.createDirectory(name: name, isAutomicCreate: false), true)
        }
        return ("", false)
    }
    // 移动目录；随便更改文件夹名称
    class func moveDir(_ oldPath: String, _ newPath: String) -> (String, Bool){
        let old_path = getDocument() + "/" + oldPath
        let new_path = getTmp() + "/" + newPath
        do {
            try manager.moveItem(atPath: old_path, toPath: new_path)
            return (new_path, true)
        } catch {
            return ("", false)
        }
    }
    
    // 复制目录 把整个目录下的所有的文件都复制，并且可以自定义新目录名称
    class func copeDir(_ oldPath: String, _ newPath: String) -> (String, Bool){
        let old_path = getDocument() + "/" + oldPath
        let new_path = getTmp() + "/" + newPath
        do {
            try manager.copyItem(atPath: old_path, toPath: new_path)
            return (new_path, true)
        } catch {
            return ("", false)
        }
    }
}
// 增删移文件
extension FileTool {
    // 创建文件: 1、先有目录，再有文件
   class func createFileWith1() -> Bool{
        let dirPath = FileTool.createDirectory(name: "CustomDir", isAutomicCreate: true) + "/myself.txt"
        let content = "hello world"
        do {
            try content.write(toFile: dirPath, atomically: true, encoding: String.Encoding.utf8)
            print("dir = \(dirPath)")
            return true
        } catch {
            print("写入失败")
            return false
        }
    }
    // 创建文件
    class func createFileWith2() -> Bool{
     let dir = FileTool.createDirectory(name: "your", isAutomicCreate: true) + "/yours1.png"
        if manager.createFile(atPath: dir, contents: nil, attributes: nil) {
            print("success = \(dir)")
            return true
        }else {
            print("failed")
            return false
        }
    }
    
    // 移动文件
    class func moveFile() -> (String, Bool) {
        
        return ("", false)
    }
    
    //删除某目录下的所有文件
    class func deleteAllFiles() -> Bool{
        return true
    }
    
    // 删除文件
    class func deleteFile() -> Bool {
        
        return true
    }
}

// https://www.jianshu.com/p/8e78c7cb8d77















